package com.sg.service.base.framework.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sg.common.util.CommonFunctionHelper;
import com.sg.dto.base.framework.req.BatchQueryMemberTestDataReq;
import com.sg.dto.base.framework.req.QueryFrameworkSpaceMemberListReq;
import com.sg.dto.base.framework.req.QueryMemberDetailReq;
import com.sg.entity.SpaceMember;
import com.sg.service.base.framework.MSpaceMemberService;
import com.wicket.okrframework.common.dto.FieldInfo;
import com.wicket.okrframework.common.exception.BizException;
import com.wicket.okrframework.dal.mapper.mbg.SpaceMemberMapper;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author ：
 * @version 1.0
 * @since 2022/1/19 15:39
 */
@Service
public class MSpaceMemberServiceImpl
        extends ServiceImpl<SpaceMemberMapper, SpaceMember>
        implements MSpaceMemberService {

    @Autowired
    SpaceMemberMapper spaceMemberMapper;

    /**
     * 1-2-05查询空间成员关系详情[167]
     * gen by moon at 4/23/2023, 11:15:20 AM
     */
    @Trace(operationName = "1-2-05查询空间成员关系详情")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public SpaceMember queryMemberDetail(QueryMemberDetailReq reqDto) {

        try {

            QueryWrapper<SpaceMember> queryWrapper = new QueryWrapper<>();
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, new ArrayList<>());
            queryWrapper.orderByDesc("create_time");
            List<SpaceMember> retList = super.list(queryWrapper);
            if (retList != null && retList.size() > 0) {
                SpaceMember retData = retList.get(0);

                return retData;
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 1-2-05新增空间成员关系[61]
     * gen by moon at 10/16/2022, 11:06:24 AM
     */
    @Trace(operationName = "1-2-05新增空间成员关系")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String addMember(SpaceMember spaceMember) {
        spaceMember.setMemberId(CommonFunctionHelper.getUid());//DISTRIBUTED_ID
        spaceMember.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        spaceMember.setCreateInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        spaceMember.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        spaceMember.setCreateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        spaceMember.setIsValid("FALSE");
        if (super.save(spaceMember)) {
            return spaceMember.getMemberId();
        } else {
            return "-1";
        }

    }

    /**
     * 1-2-05查询空间成员关系列表[64]
     * gen by moon at 10/16/2022, 11:06:25 AM
     */
    @Trace(operationName = "1-2-05查询空间成员关系列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<SpaceMember> queryFrameworkSpaceMemberList(QueryFrameworkSpaceMemberListReq reqDto) {

        try {
            QueryWrapper<SpaceMember> queryWrapper = new QueryWrapper<>();


            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(SpaceMember.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(SpaceMember.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(SpaceMember.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);

            return ((List<SpaceMember>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 1-2-05删除空间成员关系[63]
     * gen by moon at 10/16/2022, 11:06:25 AM
     */
    @Trace(operationName = "1-2-05删除空间成员关系")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteFrameworkSpaceMember(String memberId) {

        return super.removeById(memberId);
    }

    /**
     * M1批量查询空间成员测试[4859]
     * gen by moon at 10/16/2022, 11:06:28 AM
     */
    @Trace(operationName = "M1批量查询空间成员测试")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<SpaceMember> batchQueryMemberTestData(BatchQueryMemberTestDataReq reqDto) {

        try {
            QueryWrapper<SpaceMember> queryWrapper = new QueryWrapper<>();

            if (reqDto.getMemberTestDataList() == null || reqDto.getMemberTestDataList().isEmpty()) {
                return new ArrayList<>();
            }
            queryWrapper.in("user_id", reqDto.getMemberTestDataList());
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(SpaceMember.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(SpaceMember.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(SpaceMember.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);

            return ((List<SpaceMember>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * M1批量删空间成员测试[4860]
     * gen by moon at 10/16/2022, 11:06:29 AM
     */
    @Trace(operationName = "M1批量删空间成员测试")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchDeleteMemberTestData(List<String> reqDto) {

        if (reqDto == null || reqDto.size() == 0) {
            return false;
        }

        return super.removeByIds(reqDto);
    }

    /**
     * M1查询空间成员测试列表[4884]
     * gen by moon at 10/16/2022, 11:06:30 AM
     */
    @Trace(operationName = "M1查询空间成员测试列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<SpaceMember> queryMemberTestDataList(SpaceMember spaceMember) {

        try {
            QueryWrapper<SpaceMember> queryWrapper = new QueryWrapper<>();


            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(spaceMember);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(SpaceMember.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(SpaceMember.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(SpaceMember.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);

            return ((List<SpaceMember>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }
}
